#include "3DScenePanel.h"

#include <string>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QTreeWidget>
#include <osgEarth/IntersectionPicker>

C3DScenePanel::C3DScenePanel()
	: osgQt::GLWidget
	(
	//h00017
	QGLFormat(QGL::SampleBuffers)
	)
{
	setAcceptDrops(true);
	setGeometry(0, 0, 300, 100);
	setBaseSize(300, 100);
	//setWindowIcon(QIcon(":/images/free_earth.png"));
	//setMouseTracking(true);
	//setAttribute(Qt::WA_AcceptTouchEvents);
}

bool C3DScenePanel::Initialize(IRenderContext *pRenderContext)
{
	//m_rpSystemManager = new CFreeEarth(m_strDataPath);
	m_opRenderContext = pRenderContext;

	if(m_opRenderContext.valid())
	{
		m_opRenderContext->SetGraphicContext(
			new osgQt::GraphicsWindowQt(
			m_opRenderContext->CreateTraits(new osgQt::GraphicsWindowQt::WindowData(this), x(), y(), width(), height())));

		InstallFrameTimer();
		return true;
	}
	return false;
}

bool C3DScenePanel::UnInitialize()
{
	if (!m_opRenderContext.valid())
	{
		return false;
	}

	return true;
}

void C3DScenePanel::InstallFrameTimer()
{
	connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));
	m_timer.start();
}

void C3DScenePanel::paintEvent( QPaintEvent* event)
{
	if(m_opRenderContext.valid())
	{
		m_opRenderContext->Frame();
	}

	osgQt::GLWidget::paintEvent(event);
}

void C3DScenePanel::resizeEvent( QResizeEvent* e )
{
	int nX(0);
	int nY(0);
	int nWidth(0);
	int nHeight(0);

	geometry().getRect(&nX, &nY, &nWidth, &nHeight);

	nY = nHeight - nY;

	/*if(m_opRenderContext.valid())
	{
	m_rpSystemManager->ResizeGraphicsContext(nX, nY, nWidth, nHeight);
	}*/

	osgQt::GLWidget::resizeEvent(e);
}


void C3DScenePanel::dragEnterEvent( QDragEnterEvent* e )
{
	if( e->mimeData()->hasFormat("ModelPath") || qobject_cast<QTreeWidget*>(e->source()) != NULL )
		e->acceptProposedAction();
	else
		e->ignore();
}

void C3DScenePanel::dragMoveEvent( QDragMoveEvent* e )
{
	if(e->mimeData()->hasFormat("ModelPath") || qobject_cast<QTreeWidget*>(e->source()) != NULL)
	{
		QRect rect = geometry();
		int x = e->pos().x();
		int y = e->pos().y();
		y = rect.height() - y;

		/*osgEarth::IntersectionPicker picker(
		m_rpSystemManager->GetSystemService()->GetRenderContext()->GetView(), 
		m_rpSystemManager->GetSystemService()->GetRenderContext()->GetMapNode());
		osgEarth::IntersectionPicker::Hits hits;
		if(picker.pick(float(x),float(y),hits))
		{
		e->acceptProposedAction();
		return;
		}*/

	}

	e->ignore();
}

void C3DScenePanel::dropEvent( QDropEvent* e )
{
	if( false == e->mimeData()->hasFormat("ModelPath")  && qobject_cast<QTreeWidget*>(e->source()) == NULL )
	{
		return;
	}

	int nScreenX = e->pos().x();
	int nScreenY = e->pos().y();
	QRect r = geometry();
	nScreenY = r.height() - nScreenY;
	/*osg::Vec3d vecPos;
	vecPos.x() = nScreenX;
	vecPos.y() = nScreenY;*/
	//osg::Vec3d llh;
	//ScreenXY2DegreeLLH(m_rpSystemManager->GetRenderContext(), nScreenX, nScreenY, llh);
	//OSG_NOTICE << "llh x: " << llh.x() << "  y:  " << llh.y() << " z: " << llh.z() << std::endl;
	//emit SignalDropEvent(e->source(), llh);
}

bool C3DScenePanel::event( QEvent* event )
{
	return QGLWidget::event( event );
}



